//
//  OrderNumVm.swift
//  Pods
//
//  Created by eme on 2017/3/23.
//
//

import SwiftyJSON
import RxSwift
import Result
import ModelProtocol

class OrderNumVm{
    
    let backMessage = PublishSubject<OrderNumModel>()
    
    let changeMessage = PublishSubject<Void>()
    
    let error = PublishSubject<MikerError>()
    
    let getMessageCount = PublishSubject<Void>()
    
    let disposeBag = DisposeBag()
    
    init(){
        
        let request = changeMessage.map{ message in
            
            return MineApi.getOrderNum()
            
            }.shareReplay(1)
        
        let request1 = getMessageCount.map{ _ in
            
            return MineApi.getMessageCount()
            
            }.shareReplay(1)
        
        let response = request.emeRequestApiForObj(OrderNumModel.self).shareReplay(1)
        let response1 = request1.emeRequestApiForObj(OrderNumModel.self).shareReplay(1)
        
        response1.subscribe(onNext: { [unowned self] (result) in
            switch result{
            case .success(let data):
                self.backMessage.onNext(data)
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        
        response.subscribe(onNext:{[unowned self] (result) in
            switch result {
            case .success(let data):
                self.backMessage.onNext(data)
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
    }
}

